
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    int port = std::stoi(argv[1]);
    // 打开网卡
    int listenId = socket(AF_INET, SOCK_STREAM, 0);
    // 进行绑定
    struct sockaddr_in serve_addr = {0};
    serve_addr.sin_family = AF_INET;
    serve_addr.sin_addr.s_addr = INADDR_ANY;
    serve_addr.sin_port = htons(port);
    bind(listenId, (const struct sockaddr *)&serve_addr, sizeof(serve_addr));
    // 设置监听状态 --- ？？？
    listen(listenId, 8);

    while (true)
    {
        struct sockaddr_in peer;
        socklen_t len = sizeof(serve_addr);
        int id = accept(listenId, (struct sockaddr *)&peer, &len);
        std::cout << "获取了一个连接: " << inet_ntoa(peer.sin_addr) << ntohs(port) << std::endl;
    }

    return 0;
}
